<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${attach.daoPackage}.${pascalName}Dao">
    <resultMap type="${attach.beanPackage}.${pascalName}" id="${camelName}">
        <#list columns as column>
            <result column="${column.columnName}" property="${column.camelName}" javaType="${column.javaType}"/>
        </#list>
    </resultMap>

    <!-- 列名 -->
    <sql id="columns">
        <#list columns as column><#if column_index gt 0>, <#if column_index%3= 0>
        </#if></#if>T1.${column.columnName}</#list>
    </sql>

    <!-- Where子句 -->
    <sql id="where_clause">
        <#list columns as column>
            <#if "String"==column.javaType>
                <if test="${column.camelName} != null and ${column.camelName} != ''">AND T1.${column.columnName} = ${r'#{'}${column.camelName}${r'}'}</if>
            <#else>
                <if test="${column.camelName} != null">AND T1.${column.columnName} = ${r'#{'}${column.camelName}${r'}'}</if>
            </#if>
        </#list>
    </sql>

    <!-- Insert -->
    <insert id="insert" parameterType="${attach.beanPackage}.${pascalName}">
        INSERT INTO ${tableName} (
        <#list columns as column><#if column_index gt 0>,<#if column_index%3= 0>
        </#if></#if>${column.columnName}</#list>
        )VALUES(
        <#list columns as column><#if column_index gt 0>,<#if column_index%3= 0>
        </#if></#if>${r'#{'}${column.camelName}${r'}'}</#list>
        )
    </insert>

    <!-- InsertSelective -->
    <insert id="insertSelective" parameterType="${attach.beanPackage}.${pascalName}">
        INSERT INTO ${tableName} (
        <trim prefix="" prefixOverrides=",">
            <#list columns as column>
                <if test="${column.camelName} != null">${column.camelName},</if>
            </#list>
        </trim>
        )VALUES(
        <trim prefix="" prefixOverrides=",">
            <#list columns as column>
                <if test="${column.camelName} != null">${r'#{'}${column.camelName}${r'}'},</if>
            </#list>
        </trim>
        )
    </insert>

    <!-- 按照ID删除 -->
    <delete id="deleteById" parameterType="String">
        DELETE FROM ${tableName} WHERE ID= ${r'#{'}id${r'}'}
    </delete>

    <!-- 删除与之类似的JavaBean -->
    <delete id="deleteSample" parameterType="${attach.beanPackage}.${pascalName}">
        DELETE FROM ${tableName} WHERE 1=1
        <include refid="where_clause"/>
    </delete>

    <!-- updateSampleById -->
    <update id="updateSampleById" parameterType="${attach.beanPackage}.${pascalName}">
        UPDATE ${tableName}
        <trim prefix="SET" suffixOverrides=",">
            <#list columns as column>
                <if test="${column.camelName} != null">${column.columnName} = ${r'#{'}${column.camelName}${r'}'},</if>
            </#list>
        </trim>
        WHERE ID= ${r'#{'}id${r'}'}
    </update>

    <!-- 按照ID查找 -->
    <select id="selectById" parameterType="String" resultMap="${camelName}">
        SELECT
        <include refid="columns"/>
        FROM ${tableName} T1 WHERE T1.ID= ${r'#{'}id${r'}'}
    </select>

    <!-- 查找与之类似的JavaBean -->
    <select id="selectSample" parameterType="${attach.beanPackage}.${pascalName}" resultMap="${camelName}">
        SELECT
        <include refid="columns"/>
        FROM ${tableName} T1 WHERE 1=1
        <include refid="where_clause"/>
    </select>

    <!-- 查找与之类似的JavaBean -->
    <select id="selectSampleOne" parameterType="${attach.beanPackage}.${pascalName}" resultMap="${camelName}">
        SELECT
        <include refid="columns"/>
        FROM ${tableName} T1 WHERE 1=1
        <include refid="where_clause"/>
        LIMIT 0,1
    </select>

    <!-- 查询列表E.G. -->
    <select id="selectList" parameterType="map" resultType="map">
        SELECT
        <include refid="columns"/>
        FROM ${tableName} T1
        WHERE 1=1
        <include refid="where_clause"/>
    </select>
</mapper>
